<template>
  <div class="profile-container">
    <div class="profile-card">
      <h1>用户个人资料</h1>
      
      <div v-if="currentUser" class="profile-info">
        <div class="profile-item">
          <label>用户名：</label>
          <span>{{ currentUser.username }}</span>
        </div>
        <div class="profile-item">
          <label>邮箱：</label>
          <span>{{ currentUser.email }}</span>
        </div>
        <div class="profile-item">
          <label>注册时间：</label>
          <span>{{ formatDate(currentUser.createdAt) }}</span>
        </div>
        
        <button @click="handleLogout" class="logout-btn">
          退出登录
        </button>
      </div>
      
      <div v-else class="loading">
        <p>加载用户信息中...</p>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { authService } from '@/services/auth.js'

const router = useRouter()
const currentUser = ref(null)

const loadUserData = () => {
  const user = authService.getCurrentUser()
  if (user) {
    currentUser.value = user
  } else {
    // 如果用户未登录，重定向到登录页面
    router.push('/login')
  }
}

const handleLogout = () => {
  authService.logout()
  router.push('/login')
}

const formatDate = (dateString) => {
  const date = new Date(dateString)
  return date.toLocaleString('zh-CN')
}

onMounted(() => {
  loadUserData()
})
</script>

<style scoped>
.profile-container {
  min-height: 100vh;
  background-color: var(--color-background);
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 2rem;
}

.profile-card {
  background-color: white;
  border-radius: 8px;
  padding: 2rem;
  width: 100%;
  max-width: 500px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

h1 {
  color: var(--color-heading);
  margin-bottom: 1.5rem;
  text-align: center;
}

.profile-info {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.profile-item {
  display: flex;
  justify-content: space-between;
  padding: 0.5rem 0;
  border-bottom: 1px solid #eee;
}

.profile-item label {
  color: #666;
  font-weight: 500;
}

.profile-item span {
  color: var(--color-text);
}

.logout-btn {
  background-color: var(--color-pubg-primary);
  color: white;
  border: none;
  padding: 0.75rem 1.5rem;
  border-radius: 4px;
  cursor: pointer;
  font-size: 1rem;
  transition: background-color 0.3s;
  margin-top: 1.5rem;
  width: 100%;
}

.logout-btn:hover {
  background-color: var(--color-pubg-primary-dark);
}

.loading {
  text-align: center;
  color: var(--color-text);
}

@media (max-width: 768px) {
  .profile-card {
    padding: 1.5rem;
  }
  
  .profile-item {
    flex-direction: column;
    gap: 0.25rem;
  }
}
</style>